﻿using HKB.Common;
using HKB.Web;
using HKB.Web.Admin;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.WebControls;

public partial class Admin_Admin_AddLabel : Admin, IRequiresSessionState
{
    private LabelBll _lb = null;
    protected DefaultProfile profile
    {
        get
        {
            return (DefaultProfile)this.Context.Profile;
        }
    }
    protected global_asax applicationInstance
    {
        get
        {
            return (global_asax)this.Context.ApplicationInstance;
        }
    }
    protected LabelBll lb
    {
        get
        {
            LabelBll lb;
            if (this._lb == null)
            {
                this._lb = new LabelBll(this.conn);
                lb = this._lb;
            }
            else
            {
                lb = this._lb;
            }
            return lb;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!base.admin.CheckAdminPower(14))
        {
            base.WriteLog("View", Language.GetAdminMsg("Log/Access", Fetch.GetID("id").ToString()), "Label");
            base.WriteRightErrMsg();
        }
        this.LabelDataSourceSet.Attributes.Add("onchange", "display_labelTemplate(this.value,'" + this.LabelDataSourceConn.ClientID + "')");
        if (!base.IsPostBack)
        {
            this.BindPage();
        }
    }
    protected void BindPage()
    {
        DataTable dataTable = new DataTable();
        if (Fetch.GetID("id") > 0)
        {
            int iD = Fetch.GetID("id");
            this.SaveButton.Visible = true;
            dataTable = this.lb.GetLabelDefineByLabelID(iD);
            string str = string.Concat(new object[]
			{
				"?action=modify&id=",
				iD,
				"&name=",
				base.Server.UrlEncode(dataTable.Rows[0]["Name"].ToString())
			});
            this.GoToStep.Visible = true;
            this.GoToStep.Attributes.Add("onchange", "location.href=this.value");
            this.GoToStep.Items.Add(new ListItem("跳转到其它标签参数设置步骤...", "#"));
            this.GoToStep.Items.Add(new ListItem("设置标签参数", "Admin_LabelParms.aspx" + str));
            if (dataTable.Rows[0]["LabelDataType"].ToString() != "static")
            {
                this.GoToStep.Items.Add(new ListItem("设置查询语句", "Admin_LabelSqlBuild.aspx" + str));
                this.GoToStep.Items.Add(new ListItem("设置标签内容", "Admin_LabelTemplate.aspx" + str));
            }
            else
            {
                this.GoToStep.Items.Add(new ListItem("设置标签内容", "Admin_LabelTemplateStatic.aspx" + str));
            }
        }
        else
        {
            if (Fetch.Get("action") == "add")
            {
                if (!string.IsNullOrEmpty(Fetch.Get("name")))
                {
                    string name = Fetch.Get("name");
                    if (this.Session[name] != null)
                    {
                        dataTable = (DataTable)this.Session[name];
                    }
                }
            }
        }
        if (dataTable.Rows.Count == 1)
        {
            DataRow dataRow = dataTable.Rows[0];
            this.LabelName.Text = dataRow["Name"].ToString();
            this.LabelClass.Text = dataRow["Type"].ToString();
            this.LabelTemplateType.SelectedValue = dataRow["LabelType"].ToString();
            this.LabelIntro.Text = dataRow["LabelIntro"].ToString();
            this.LabelDataSourceSet.SelectedValue = dataRow["LabelDataType"].ToString();
            this.LabelCacheTime.Text = dataRow["CacheTime"].ToString();
            this.LabelDataSourceConn.Text = dataRow["ConnStr"].ToString();
            if (dataTable.Columns.Contains("OutPutType") && dataRow["OutPutType"].ToString() != null && dataRow["OutPutType"].ToString() != "")
            {
                this.LabelOutPutType.SelectedValue = dataRow["OutPutType"].ToString();
                if (dataTable.Columns.Contains("StaticOutPut") && dataRow["StaticOutPut"].ToString() != null && dataRow["StaticOutPut"].ToString() != "")
                {
                    string[] array = dataRow["StaticOutPut"].ToString().Split(new char[]
					{
						'.'
					});
                    if (array.Length == 2)
                    {
                        this.StaticLabelOutPutName.Text = array[0];
                        this.StaticLabelOutPutType.SelectedValue = array[1];
                    }
                }
            }
            else
            {
                this.LabelOutPutType.SelectedValue = "activity";
            }
            if (dataRow["OutType"].ToString() != "")
            {
                this.LabelTemplateType.SelectedValue = dataRow["OutType"].ToString();
            }
        }
        DataTable labelType = this.lb.GetLabelType();
        if (labelType.Rows.Count > 0)
        {
            this.LabelClassSelect.DataSource = labelType;
            this.LabelClassSelect.DataTextField = "Type";
            this.LabelClassSelect.DataValueField = "Type";
            this.LabelClassSelect.DataBind();
            this.LabelClassSelect.Attributes.Add("onchange", "document.getElementById('" + this.LabelClass.ClientID + "').value=this.value");
        }
        this.LabelOutPutType.Attributes.Add("onchange", "ShowLabelType(this.value)");
        this.show_label_type_js.Text = "<script type=\"text/javascript\">ShowLabelType('" + this.LabelOutPutType.SelectedValue + "')</script>";
    }
    protected void NextSetp_Click(object sender, EventArgs e)
    {
        if (this.LabelName.Text == "" || this.LabelName.Text == null)
        {
            this.LabelNameErr.Text = "<font color='red'>请输入标签名称！</font>";
        }
        else
        {
            this.LabelNameErr.Text = "";
            if (this.LabelClass.Text == "" || this.LabelClass.Text == null)
            {
                this.LabelClassErr.Text = "<font color='red'>请输入标签分类，方便查找！</font>";
            }
            else
            {
                this.LabelClassErr.Text = "";
                DataTable labelByLabelName = this.lb.GetLabelByLabelName(this.LabelName.Text);
                if (labelByLabelName.Rows.Count > 0)
                {
                    if ((Fetch.GetID("ID") != Convert.ToInt32(labelByLabelName.Rows[0]["ID"]) && Fetch.Get("action") == "modify") || Fetch.Get("action") == "add")
                    {
                        this.LabelNameErr.Text = "<font color='red'>标签名称重复！</font>";
                        return;
                    }
                    this.LabelNameErr.Text = "";
                }
                else
                {
                    this.LabelNameErr.Text = "";
                }
                DataTable dataTable = new DataTable();
                dataTable.Columns.Add("Name");
                dataTable.Columns.Add("Type");
                dataTable.Columns.Add("LabelType");
                dataTable.Columns.Add("LabelIntro");
                dataTable.Columns.Add("OutType");
                dataTable.Columns.Add("LabelDataType");
                dataTable.Columns.Add("CacheTime");
                dataTable.Columns.Add("ConnStr");
                dataTable.Columns.Add("ParmArray");
                dataTable.Columns.Add("LabelContent");
                dataTable.Columns.Add("SqlStr");
                dataTable.Columns.Add("PageSqlStr");
                dataTable.Columns.Add("EnablePage");
                dataTable.Columns.Add("OutPutType");
                dataTable.Columns.Add("StaticOutPut");
                DataRow dataRow = dataTable.NewRow();
                dataRow["Name"] = this.LabelName.Text;
                dataRow["Type"] = this.LabelClass.Text;
                dataRow["LabelType"] = this.LabelTemplateType.SelectedValue;
                dataRow["LabelIntro"] = this.LabelIntro.Text;
                dataRow["LabelDataType"] = this.LabelDataSourceSet.SelectedValue;
                dataRow["CacheTime"] = this.LabelCacheTime.Text;
                dataRow["ConnStr"] = this.LabelDataSourceConn.Text;
                dataRow["OutType"] = this.LabelTemplateType.SelectedValue;
                if (this.LabelTemplateType.SelectedValue == null || this.LabelTemplateType.SelectedValue == "")
                {
                    dataRow["OutType"] = "simple";
                }
                dataRow["OutPutType"] = this.LabelOutPutType.SelectedValue;
                dataRow["StaticOutPut"] = this.StaticLabelOutPutName.Text + "." + this.StaticLabelOutPutType.SelectedValue;
                dataTable.Rows.Add(dataRow);
                this.Session[this.LabelName.Text] = dataTable;
                if (Fetch.GetID("id") > 0)
                {
                    base.Response.Redirect("Admin_LabelParms.aspx?action=modify&id=" + base.Request["id"] + "&name=" + base.Server.UrlEncode(this.LabelName.Text));
                }
                else
                {
                    base.Response.Redirect("Admin_LabelParms.aspx?action=add&id=0&name=" + base.Server.UrlEncode(this.LabelName.Text));
                }
            }
        }
    }
    protected void SaveButton_Click(object sender, EventArgs e)
    {
        int iD = Fetch.GetID("ID");
        if (iD == 0)
        {
            this.WriteErrMsg("", Language.Get("Common/LostParam", "ID"), "");
        }
        DataTable labelByLabelName = this.lb.GetLabelByLabelName(this.LabelName.Text);
        if (labelByLabelName.Rows.Count > 0)
        {
            if (iD != Convert.ToInt32(labelByLabelName.Rows[0]["ID"]))
            {
                this.LabelNameErr.Text = "<font color='red'>标签名称重复！</font>";
                return;
            }
            this.LabelNameErr.Text = "";
        }
        else
        {
            this.LabelNameErr.Text = "";
        }
        DataTable labelDefineByLabelID = this.lb.GetLabelDefineByLabelID(iD);
        DataRow dataRow = labelDefineByLabelID.Rows[0];
        string text = this.LabelName.Text;
        string text2 = this.LabelClass.Text;
        string text3 = this.LabelIntro.Text;
        string selectedValue = this.LabelDataSourceSet.SelectedValue;
        string text4 = this.LabelCacheTime.Text;
        string text5 = this.LabelTemplateType.SelectedValue;
        string text6 = this.LabelDataSourceConn.Text;
        string selectedValue2 = this.LabelOutPutType.SelectedValue;
        string labelStaticOutPut = this.StaticLabelOutPutName.Text + "." + this.StaticLabelOutPutType.SelectedValue;
        string[,] parmArray = new string[0, 0];
        if (text5 == null || text5 == "")
        {
            text5 = "simple";
        }
        if (this.lb.UpdateLabel(iD, text, text2, text3, selectedValue, text4, text5, text6, "", "", "", "", selectedValue2, labelStaticOutPut, parmArray))
        {
            this.WriteSuccessMsg("", Language.GetAdminMsg("Common/EditSuccess"), "Admin_Lable.aspx");
        }
    }
}
